// Camera.h: interface for the CCamera class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_CAMERA_H__DB79F3CB_5674_4C59_BFDB_EDA18168372C__INCLUDED_)
#define AFX_CAMERA_H__DB79F3CB_5674_4C59_BFDB_EDA18168372C__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "..\COMMON\cTypes.h"	// Added by ClassView
#include "../Common/RMSThread.h"
#include "ClientCount.h"

class CBaseServer;
class CLiveCamera;
class CVideoCameraStream;
class CWaveCameraStream;

typedef CLiveCamera* LPCLiveCamera;
typedef CVideoCameraStream* LPCVideoCameraStream;
typedef CWaveCameraStream* LPCWaveCameraStream;

class CVideoCameraStream : public CRMSThread
{
public:
	CBaseServer		*pBaseServer;
	LPCLiveCamera	m_pOwner;
	CVideoCameraStream(CBaseServer *p=0);
	virtual ~CVideoCameraStream();

	BOOL Init(SOCKADDR_IN saVideo, LPCLiveCamera pOwner);
	BOOL Stop();
	BOOL Restart();
	BOOL IsActive() const;

	BOOL Execute();
	BOOL CloseProcess();
	DWORD Terminate(BOOL bCritical=FALSE);
	DWORD OnTerminate();
	DWORD tcpRing(DWORD* ring);
	void EnableNoVideo(BOOL bEnable);
	void SetCaller(char* pCaller, BOOL bEnableRing);
	void SetEventHandlers(HANDLE h1, HANDLE h2);
protected:
	DWORD ThreadFunc();
	int tcpReceiveData(SOCKET& Socket,BOOL& bTerminated, void* data, int Len);
	BOOL tcpTAGRequest(SOCKET& Socket);
private:

	BOOL m_bActive;

	SOCKET		m_tcpSocket;
	SOCKADDR_IN m_saCamera;
	SOCKADDR_IN m_saServer;
	int			m_nUDPCameraPort;
	
	DWORD tcpRequestFormat();

	//	Data
	BYTE*	m_pData;

	// ring info
	BOOL    m_bSupportRing;
	BOOL	m_bEnableRing;

	BOOL m_bNoVideoEnabled;

	HANDLE success;
	HANDLE failure;
	char	m_szCaller[1000];
};

class CWaveCameraStream : public CRMSThread  
{
public:
	LPCLiveCamera m_pOwner;
	CBaseServer		*pBaseServer;
	CWaveCameraStream(CBaseServer *p=0);
	virtual ~CWaveCameraStream();

	BOOL Init(SOCKADDR_IN saWave, LPCLiveCamera pOwner);
	BOOL Stop();
	BOOL Restart();
	BOOL IsActive() const;


	BOOL Execute();
	BOOL CloseProcess();	
	DWORD OnTerminate();

protected:
	DWORD ThreadFunc();
private:

	BOOL m_bActive;

	SOCKET		m_udpSocket;
	SOCKADDR_IN m_saCamera;
	SOCKADDR_IN m_saServer;

//	Data
	BYTE*	m_pData;
};

class CLiveCamera
{
public:
	CBaseServer		*pBaseServer;
	CLiveCamera(CBaseServer	*p);
	virtual ~CLiveCamera();

	BOOL Init(int nCameraID, SOCKADDR_IN saVideo);
	BOOL Stop();
	BOOL Update();

	int GetID() const;
	BOOL IsActive() const;

	BOOL SetIP(const char* szIP);
	char*  GetIP();

	void EnableNoVideo(BOOL bEnable);

	CWaveCameraStream	m_waveStream;
	CVideoCameraStream	m_videoStream;
	DWORD lastReceivedVideoTime,lastReceivedAudioTime;
private:
	int		m_nCameraID;
	SOCKADDR_IN m_saVideo;
	BOOL	m_bActive;

	char	m_szIP[255];
};

#endif // !defined(AFX_CAMERA_H__DB79F3CB_5674_4C59_BFDB_EDA18168372C__INCLUDED_)
